package com.railway.switchs.task;

import com.railway.switchs.domain.dto.business.RepairTask;
import com.railway.switchs.domain.dto.business.SysNews;
import com.railway.switchs.domain.query.business.RepairTaskQuery;
import com.railway.switchs.domain.query.business.SysNewsQuery;
import com.railway.switchs.enums.EnumDateStyle;
import com.railway.switchs.enums.EnumNewsType;
import com.railway.switchs.enums.EnumRepairType;
import com.railway.switchs.enums.EnumRunType;
import com.railway.switchs.service.business.IRepairTaskService;
import com.railway.switchs.service.business.ISysNewsService;
import com.railway.switchs.utils.DateUtil;
import com.railway.switchs.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.helper.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @ClassName TimeTask
 * @Author 魏梓晟
 * @Version 1.0
 **/
@Component
@Slf4j
public class TimeTask {

    @Autowired
    private ISysNewsService sysNewsService;

    @Autowired
    private IRepairTaskService repairTaskService;

    /**
     * 定时任务，校验新闻和检修任务是否超时（两分钟执行一次）
     */
    @Scheduled(fixedRate=120000)
    public void checkoutTime(){

        SysNewsQuery sysNewsQuery = new SysNewsQuery();
        sysNewsQuery.setEndTimeEnd(DateUtil.DateToString(new Date(), EnumDateStyle.YYYY_MM_DD_HH_MM_SS));
        sysNewsQuery.setNewsType(EnumNewsType.HAS_ANNOUNCED.getCode());
        List<SysNews> sysNewsList = sysNewsService.getList(sysNewsQuery);
        if(StringUtil.isNotEmpty(sysNewsList)){
            for(SysNews sysNews : sysNewsList){
                sysNews.setNewsType(EnumNewsType.HAS_RESCINDED.getCode());
                sysNewsService.edit(sysNews);
            }
        }

        RepairTaskQuery repairTaskQuery = new RepairTaskQuery();
        repairTaskQuery.setCloseTimeEnd(DateUtil.DateToString(new Date(), EnumDateStyle.YYYY_MM_DD_HH_MM_SS));
        repairTaskQuery.setTaskType(EnumRepairType.UNHANDLE.getCode());
        List<RepairTask> repairTaskList = repairTaskService.getList(repairTaskQuery);
        if(StringUtil.isNotEmpty(repairTaskList)){
            for(RepairTask repairTask : repairTaskList){
                repairTask.setTaskType(EnumRepairType.OVERTIME.getCode());
                repairTaskService.edit(repairTask);
            }
        }
    }
}
